// packageB/pages/buyNow/buyNow.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    checked: true,
    product: '',
    picPath: app.globalData.picPath,
    canPay: -1,
    shareId: 0,
  },
  pay() {
    if (this.data.canPay == 1) {
      //已购买
      ks.redirectTo({
        url: '/pages/index/index',
      })
      return;
    }

    if (app.globalData.teacherId != 0) {
      this.payHandle()
    } else {
      //通过分享来的
      // 登录
      var that = this;
      ks.login({
        success: res => {
          // 发送 res.code 到后台换取 openId, sessionKey, unionId
          var url = app.globalData.url + "user/ksGetOpenId";
          app.wxRequest(url, {
            code: res.code,
          }, res => {
            console.log("实现执行这里吗");
            console.log(res);
            if (res.flag) {
              var userInfo = res.data.teacher;
              app.globalData.openId = userInfo.openId;
              app.globalData.teacherId = userInfo.teacherId;
              app.globalData.isLogin = 1;
              ks.setStorageSync('isLogin', 1);
              ks.setStorageSync('openId', userInfo.openId);
              ks.setStorageSync('teacherId', userInfo.teacherId);
              ks.setStorageSync('teacherDetail', userInfo);
              app.globalData.teacherDetail = userInfo;
              console.log(app.globalData);

              that.payHandle()
            }
          }, err => { })
        }
      })
    }
  },


  payHandle() {
    var url = app.globalData.url + "goldShop/ksAddMainOrder";
    var that = this;
    var product = that.data.product

    console.log(product);
    console.log(product);
    app.wxRequest(url, {
      teacherId: app.globalData.teacherId,
      productId: product.productId,
      realSum: product.realSum,
      // realSum: 0.01,
      totalSum: product.originalCost,
      productName: product.productName,
      openId: app.globalData.openId,
      shareId: that.data.shareId
    }, res => {
      console.log(res);
      var datas = res.data.datas
      console.log(datas);
      ks.pay({
        serviceId: 1,
        orderInfo: datas,
        success: function (res) {
          ks.showModal({
            title: '提示',
            content: '购买成功',
            showCancel: false,
            complete: (res) => {
              if (res.confirm) {
                ks.switchTab({
                  url: '/pages/index/index'
                })
              }
            }
          })
        },
        fail: function (res) {
          console.log(res);
        },
        complete: function (res) {
          console.log(res);
        }
      })
    })


  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    console.log(options);

    // var str = '22_352'.split("_");
    // var shareId = str[1];
    // var ptoductId = str[0];
    // this.setData({
    //   shareId: shareId
    // })
    // this.getProduct(ptoductId);

    // return


    if (options.scene) {
      var str = options.scene.split("_");
      var shareId = str[1];
      var ptoductId = str[0];
      this.setData({
        shareId: shareId
      })
      this.getProduct(ptoductId);
    } else {
      var product = JSON.parse(options.product)
      this.dealNum(product);
    }
  },
  dealNum(product) {
    product.realSum = product.productPrice.toFixed(2)
    product.productPrice = product.productPrice.toFixed(2)
    this.setData({
      product: product
    })
    console.log(product);
  },
  getProduct(id) {
    var that = this;
    var url = app.globalData.url + "goldShop/getProductOne";
    app.wxRequest(url, {
      productId: id,
      userId: app.globalData.teacherId,
    }, res => {
      var product = res.data.product;
      that.setData({
        canPay: res.data.flag
      })
      that.dealNum(product);
    }, err => { });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  onShareAppMessage() {
    let myObj = {
      title: this.data.product.productName,
      path: "/packageA/pages/buy/buy?scene=" + this.data.product.productId + "_" + app.globalData.teacherId,
      // imageUrl: '/img/shareImg.png'
    }
    return myObj;
  },
  onShareTimeline() {
    let myObj = {
      title: this.data.product.productName,
      path: "/packageA/pages/buy/buy?scene=" + this.data.product.productId + "_" + app.globalData.teacherId,
    }
    return myObj;
  },
})